package com.yunxi.service.localService.impl.sto;


import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.GUIDGenerator;
import com.yunxi.dao.order.VehicleMaintainRecordMapper;
import com.yunxi.model.order.PmsVehicleMaintainRecord;
import com.yunxi.service.localService.sto.VehicleMaintainItemService;
import com.yunxi.service.localService.sto.VehicleMaintainRecordService;
import com.yunxi.sto.pojo.VehicleStockVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * <p>
 * 经销商车辆库存 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-05-29
 */
@Service
public class VehicleMaintainRecordServiceImpl extends ServiceImplPlus<VehicleMaintainRecordMapper, PmsVehicleMaintainRecord> implements VehicleMaintainRecordService {

    @Autowired
    @Qualifier(value = "vehicleMaintainItemServiceImpl")
    VehicleMaintainItemService vehicleMaintainItemService;

    @Override
    public void saveRecord(VehicleStockVO vehicleStockVO) {
        PmsVehicleMaintainRecord  maintainRecord = new PmsVehicleMaintainRecord();
        Integer count =  baseMapper.selectByVin(vehicleStockVO.getVin());
        Integer recordCount = count+1;
        if(count!=null){
            if(recordCount % 6 == 0){
                maintainRecord.setMaintainPeriod("6");
            }else{
                maintainRecord.setMaintainPeriod(recordCount%6+"");
            }
        }
        else{
            maintainRecord.setMaintainPeriod("1");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        maintainRecord.setStartDate(simpleDateFormat.format(new Date()));
        maintainRecord.setVin(vehicleStockVO.getVin());
        maintainRecord.setMaintainDate(vehicleStockVO.getMaintainDate());
        Integer differDay =0;
        maintainRecord.setDifferDay(differDay);
        maintainRecord.setMaintainId(GUIDGenerator.getPrimaryKey());
        baseMapper.insert(maintainRecord);
        vehicleMaintainItemService.saveBatchByMaintainId(maintainRecord,vehicleStockVO.getListVehicleMaintainItem());
    }
}
